| | | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace GDX.Collections |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// An adapter collection for external data arrays that allows constant-time insertion, deletion, and lookup by |
| | | 12 | | /// handle, as well as array-like iteration. |
| | | 13 | | /// </summary> |
| | | 14 | | public struct SparseSet |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Holds references to the sparse array for swapping indices. |
| | | 18 | | /// </summary> |
| | | 19 | | public int[] DenseArray; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Holds references to dense array indices. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// Its own indices are claimed and freed via a free-list. |
| | | 26 | | /// </remarks> |
| | | 27 | | public int[] SparseArray; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// How many indices are being used currently? |
| | | 31 | | /// </summary> |
| | | 32 | | public int Count; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The first free (currently unused) index in the sparse array. |
| | | 36 | | /// </summary> |
| | | 37 | | public int FreeIndex; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Create a <see cref="SparseSet" /> with an <paramref name="initialCapacity" />. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> |
| | | 43 | | public SparseSet(int initialCapacity) |
| | 38 | 44 | | { |
| | 38 | 45 | | DenseArray = new int[initialCapacity]; |
| | 38 | 46 | | SparseArray = new int[initialCapacity]; |
| | 38 | 47 | | Count = 0; |
| | 38 | 48 | | FreeIndex = 0; |
| | | 49 | | |
| | 228 | 50 | | for (int i = 0; i < initialCapacity; i++) |
| | 76 | 51 | | { |
| | 76 | 52 | | DenseArray[i] = -1; |
| | 76 | 53 | | SparseArray[i] = i + 1; |
| | 76 | 54 | | } |
| | 38 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Create a <see cref="SparseSet" /> with an <paramref name="initialCapacity" />. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="initialCapacity">The initial capacity of the sparse and dense int arrays.</param> |
| | | 61 | | /// <param name="versionArray">Array containing version numbers to check sparse references against.</param> |
| | | 62 | | public SparseSet(int initialCapacity, out ulong[] versionArray) |
| | 16 | 63 | | { |
| | 16 | 64 | | DenseArray = new int[initialCapacity]; |
| | 16 | 65 | | SparseArray = new int[initialCapacity]; |
| | 16 | 66 | | versionArray = new ulong[initialCapacity]; |
| | 16 | 67 | | Count = 0; |
| | 16 | 68 | | FreeIndex = 0; |
| | | 69 | | |
| | 116 | 70 | | for (int i = 0; i < initialCapacity; i++) |
| | 42 | 71 | | { |
| | 42 | 72 | | DenseArray[i] = -1; |
| | 42 | 73 | | SparseArray[i] = i + 1; |
| | 42 | 74 | | versionArray[i] = 1; |
| | 42 | 75 | | } |
| | 16 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="expandBy">How many indices to expand by.</param> |
| | | 82 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | | 83 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | | 84 | | /// <returns>True if the index pool expanded.</returns> |
| | | 85 | | public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex) |
| | 6 | 86 | | { |
| | 6 | 87 | | int indexToClaim = FreeIndex; |
| | 6 | 88 | | int currentCapacity = SparseArray.Length; |
| | 6 | 89 | | bool needsExpansion = false; |
| | | 90 | | |
| | 6 | 91 | | if (indexToClaim >= currentCapacity) |
| | 3 | 92 | | { |
| | | 93 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 3 | 94 | | needsExpansion = true; |
| | | 95 | | |
| | 3 | 96 | | int newCapacity = currentCapacity + expandBy; |
| | | 97 | | |
| | 3 | 98 | | int[] newSparseArray = new int[newCapacity]; |
| | 3 | 99 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 3 | 100 | | SparseArray = newSparseArray; |
| | | 101 | | |
| | 3 | 102 | | int[] newDenseArray = new int[newCapacity]; |
| | 3 | 103 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 3 | 104 | | DenseArray = newDenseArray; |
| | | 105 | | |
| | 36 | 106 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 15 | 107 | | { |
| | 15 | 108 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 15 | 109 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 15 | 110 | | } |
| | 3 | 111 | | } |
| | | 112 | | |
| | 6 | 113 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 6 | 114 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 6 | 115 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 6 | 116 | | denseIndex = Count; |
| | | 117 | | |
| | 6 | 118 | | ++Count; |
| | 6 | 119 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 120 | | |
| | 6 | 121 | | sparseIndex = indexToClaim; |
| | 6 | 122 | | return needsExpansion; |
| | 6 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="expandBy">How many indices to expand by.</param> |
| | | 129 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | | 130 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | | 131 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | | 132 | | /// <param name="version">Enables detection of use-after-free errors when using the sparse index as a reference. |
| | | 133 | | /// <returns>True if the index pool expanded.</returns> |
| | | 134 | | public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex, ref ulong[] versionArray, |
| | 0 | 135 | | { |
| | 0 | 136 | | int indexToClaim = FreeIndex; |
| | 0 | 137 | | int currentCapacity = SparseArray.Length; |
| | 0 | 138 | | bool needsExpansion = false; |
| | | 139 | | |
| | 0 | 140 | | if (indexToClaim >= currentCapacity) |
| | 0 | 141 | | { |
| | | 142 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 143 | | needsExpansion = true; |
| | | 144 | | |
| | 0 | 145 | | int newCapacity = currentCapacity + expandBy; |
| | | 146 | | |
| | 0 | 147 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 148 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 149 | | SparseArray = newSparseArray; |
| | | 150 | | |
| | 0 | 151 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 152 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 153 | | DenseArray = newDenseArray; |
| | | 154 | | |
| | 0 | 155 | | ulong[] newVersionArray = new ulong[newCapacity]; |
| | 0 | 156 | | Array.Copy(versionArray, 0, newVersionArray, 0, currentCapacity); |
| | 0 | 157 | | versionArray = newVersionArray; |
| | | 158 | | |
| | 0 | 159 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 160 | | { |
| | 0 | 161 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 162 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 163 | | newVersionArray[i] = 1; |
| | 0 | 164 | | } |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 168 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 169 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | 0 | 170 | | denseIndex = Count; |
| | | 171 | | |
| | 0 | 172 | | version = versionArray[indexToClaim]; |
| | | 173 | | |
| | 0 | 174 | | ++Count; |
| | 0 | 175 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 176 | | |
| | 0 | 177 | | sparseIndex = indexToClaim; |
| | 0 | 178 | | return needsExpansion; |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Adds a sparse/dense index pair to the set without checking if the set needs to expand. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | | 185 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | | 186 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 187 | | public void AddUnchecked(out int sparseIndex, out int denseIndex) |
| | 20 | 188 | | { |
| | 20 | 189 | | int indexToClaim = FreeIndex; |
| | 20 | 190 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 19 | 191 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 19 | 192 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 193 | | |
| | 19 | 194 | | sparseIndex = indexToClaim; |
| | 19 | 195 | | denseIndex = Count; |
| | 19 | 196 | | ++Count; |
| | 19 | 197 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 19 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Adds a sparse/dense index pair to the set without checking if the set needs to expand. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="sparseIndex">The sparse index allocated.</param> |
| | | 204 | | /// <param name="denseIndex">The dense index allocated.</param> |
| | | 205 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | | 206 | | /// <param name="version">Enables detection of use-after-free errors when using the sparse index as a reference. |
| | | 207 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 208 | | public void AddUnchecked(out int sparseIndex, out int denseIndex, ulong[] versionArray, out ulong version) |
| | 24 | 209 | | { |
| | 24 | 210 | | int indexToClaim = FreeIndex; |
| | 24 | 211 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 23 | 212 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 23 | 213 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 214 | | |
| | 23 | 215 | | version = versionArray[indexToClaim]; |
| | 23 | 216 | | sparseIndex = indexToClaim; |
| | 23 | 217 | | denseIndex = Count; |
| | | 218 | | |
| | 23 | 219 | | ++Count; |
| | 23 | 220 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | 23 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Gets the value of the sparse array at the given index without any data validation. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <param name="sparseIndex">The index to check in the sparse array.</param> |
| | | 227 | | /// <returns>The dense index at the given sparse index.</returns> |
| | | 228 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 229 | | public int GetDenseIndexUnchecked(int sparseIndex) |
| | 1 | 230 | | { |
| | 1 | 231 | | return SparseArray[sparseIndex]; |
| | 1 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Gets the value of the sparse array at the given index, |
| | | 236 | | /// or -1 if the dense and sparse indices don't point to each other or if the dense index is outside the den |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | | 239 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | | 240 | | public int GetDenseIndexWithBoundsCheck(int sparseIndex) |
| | 4 | 241 | | { |
| | 4 | 242 | | if (sparseIndex >= 0 && sparseIndex < SparseArray.Length) |
| | 2 | 243 | | { |
| | 2 | 244 | | int denseIndex = SparseArray[sparseIndex]; |
| | | 245 | | |
| | 2 | 246 | | if (denseIndex < Count && denseIndex >= 0) |
| | 1 | 247 | | { |
| | 1 | 248 | | int sparseIndexAtDenseIndex = DenseArray[denseIndex]; |
| | | 249 | | |
| | 1 | 250 | | if (sparseIndex == sparseIndexAtDenseIndex) |
| | 1 | 251 | | { |
| | 1 | 252 | | return denseIndex; |
| | | 253 | | } |
| | 0 | 254 | | } |
| | 1 | 255 | | } |
| | | 256 | | |
| | 3 | 257 | | return -1; |
| | 4 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Gets the value of the sparse array at the given index, |
| | | 262 | | /// or -1 if the version number does not match. |
| | | 263 | | /// </summary> |
| | | 264 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | | 265 | | /// <param name="version">The version number associated with the sparse index.</param> |
| | | 266 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | | 267 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | | 268 | | public int GetDenseIndexWithVersionCheck(int sparseIndex, ulong version, ulong[] versionArray) |
| | 4 | 269 | | { |
| | 4 | 270 | | int denseIndex = SparseArray[sparseIndex]; |
| | 4 | 271 | | ulong versionAtSparseIndex = versionArray[sparseIndex]; |
| | | 272 | | |
| | 4 | 273 | | if (version == versionAtSparseIndex) |
| | 2 | 274 | | { |
| | 2 | 275 | | return denseIndex; |
| | | 276 | | } |
| | | 277 | | |
| | 2 | 278 | | return -1; |
| | 4 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Gets the value of the sparse array at the given index, |
| | | 283 | | /// or -1 if the given sparse index is invalid.. |
| | | 284 | | /// </summary> |
| | | 285 | | /// <param name="sparseIndex">The index in the sparse array to check against.</param> |
| | | 286 | | /// <param name="version">The version number associated with the sparse index.</param> |
| | | 287 | | /// <param name="versionArray">The array containing the version number to check against.</param> |
| | | 288 | | /// <returns>The dense index pointed to by the current sparse index, or -1 if invalid.</returns> |
| | | 289 | | public int GetDenseIndexWithBoundsAndVersionCheck(int sparseIndex, ulong version, ulong[] versionArray) |
| | 7 | 290 | | { |
| | 7 | 291 | | if (sparseIndex >= 0 && sparseIndex < SparseArray.Length) |
| | 5 | 292 | | { |
| | 5 | 293 | | int denseIndex = SparseArray[sparseIndex]; |
| | 5 | 294 | | ulong versionAtSparseIndex = versionArray[sparseIndex]; |
| | | 295 | | |
| | 5 | 296 | | if (versionAtSparseIndex == version && denseIndex < Count && denseIndex >= 0) |
| | 2 | 297 | | { |
| | 2 | 298 | | int sparseIndexAtDenseIndex = DenseArray[denseIndex]; |
| | | 299 | | |
| | 2 | 300 | | if (sparseIndex == sparseIndexAtDenseIndex) |
| | 2 | 301 | | { |
| | 2 | 302 | | return denseIndex; |
| | | 303 | | } |
| | 0 | 304 | | } |
| | 3 | 305 | | } |
| | | 306 | | |
| | 5 | 307 | | return -1; |
| | 7 | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Frees the allocated entry corresponding to the sparse index. |
| | | 312 | | /// WARNING: Will not protect against accidentally removing twice if the index in question was recycled betw |
| | | 313 | | /// <param name="sparseIndexToRemove">The sparse index corresponding to the entry to remove.</param> |
| | | 314 | | /// <param name="dataIndexToSwapTo">Replace the data array value at this index with the data array value at inde |
| | | 315 | | /// <param name="dataIndexToSwapFrom"> |
| | | 316 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 317 | | /// value at dataIndexToSwapTo. |
| | | 318 | | /// </param> |
| | | 319 | | /// </summary> |
| | | 320 | | /// <returns>True if the entry was valid and thus removed.</returns> |
| | | 321 | | public bool RemoveWithBoundsCheck(ref int sparseIndexToRemove, out int dataIndexToSwapFrom, out int dataIndexToS |
| | 4 | 322 | | { |
| | 4 | 323 | | dataIndexToSwapFrom = -1; |
| | 4 | 324 | | dataIndexToSwapTo = -1; |
| | 4 | 325 | | bool didRemove = false; |
| | 4 | 326 | | if (sparseIndexToRemove >= 0 && sparseIndexToRemove < SparseArray.Length) |
| | 2 | 327 | | { |
| | 2 | 328 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | | 329 | | |
| | 2 | 330 | | if (denseIndexToRemove >= 0 && denseIndexToRemove < Count) |
| | 1 | 331 | | { |
| | 1 | 332 | | int sparseIndexAtDenseIndex = DenseArray[denseIndexToRemove]; |
| | 1 | 333 | | int newLength = Count - 1; |
| | 1 | 334 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 335 | | |
| | 1 | 336 | | if (denseIndexToRemove < Count && sparseIndexAtDenseIndex == sparseIndexToRemove) |
| | 1 | 337 | | { |
| | 1 | 338 | | didRemove = true; |
| | | 339 | | // Swap the entry being removed with the last entry. |
| | 1 | 340 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 341 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 342 | | |
| | 1 | 343 | | dataIndexToSwapFrom = newLength; |
| | 1 | 344 | | dataIndexToSwapTo = denseIndexToRemove; |
| | | 345 | | |
| | | 346 | | // Clear the dense index, for debugging purposes |
| | 1 | 347 | | DenseArray[newLength] = -1; |
| | | 348 | | |
| | | 349 | | // Add the sparse index to the free list. |
| | 1 | 350 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 351 | | FreeIndex = sparseIndexToRemove; |
| | | 352 | | |
| | 1 | 353 | | Count = newLength; |
| | 1 | 354 | | } |
| | 1 | 355 | | } |
| | 2 | 356 | | } |
| | | 357 | | |
| | 4 | 358 | | sparseIndexToRemove = -1; |
| | | 359 | | |
| | 4 | 360 | | return didRemove; |
| | 4 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Removes the allocated entry corresponding to the sparse index. |
| | | 365 | | /// Indicates which dense indices were swapped as a result of removing the entry. |
| | | 366 | | /// </summary> |
| | | 367 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | | 368 | | /// <param name="version"> |
| | | 369 | | /// The version number of the int used to access the sparse index. Used to guard against accessing |
| | | 370 | | /// indices that have been removed and reused. |
| | | 371 | | /// </param> |
| | | 372 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | | 373 | | /// <param name="dataIndexToSwapTo">Replace the data array value at this index with the data array value at inde |
| | | 374 | | /// <param name="dataIndexToSwapFrom"> |
| | | 375 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 376 | | /// value at dataIndexToSwapTo. |
| | | 377 | | /// </param> |
| | | 378 | | /// <returns>Whether or not the remove attempt succeeded.</returns> |
| | | 379 | | public bool RemoveWithVersionCheck(int sparseIndexToRemove, ulong version, ulong[] versionArray, |
| | | 380 | | out int dataIndexToSwapFrom, out int dataIndexToSwapTo) |
| | 2 | 381 | | { |
| | 2 | 382 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 2 | 383 | | ulong versionAtSparseIndex = versionArray[sparseIndexToRemove]; |
| | | 384 | | |
| | 2 | 385 | | dataIndexToSwapFrom = -1; |
| | 2 | 386 | | dataIndexToSwapTo = -1; |
| | | 387 | | |
| | 2 | 388 | | bool succeeded = versionAtSparseIndex == version; |
| | | 389 | | |
| | 2 | 390 | | if (succeeded) |
| | 1 | 391 | | { |
| | 1 | 392 | | int newLength = Count - 1; |
| | 1 | 393 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 394 | | |
| | | 395 | | // Swap the entry being removed with the last entry. |
| | 1 | 396 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 397 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 398 | | |
| | | 399 | | // Clear the dense index, for debugging purposes |
| | 1 | 400 | | DenseArray[newLength] = -1; |
| | | 401 | | |
| | | 402 | | // Add the sparse index to the free list. |
| | 1 | 403 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 404 | | versionArray[sparseIndexToRemove] = versionArray[sparseIndexToRemove] + 1; |
| | 1 | 405 | | FreeIndex = sparseIndexToRemove; |
| | | 406 | | |
| | 1 | 407 | | Count = newLength; |
| | | 408 | | |
| | 1 | 409 | | dataIndexToSwapTo = denseIndexToRemove; |
| | 1 | 410 | | dataIndexToSwapFrom = newLength; |
| | 1 | 411 | | } |
| | | 412 | | |
| | 2 | 413 | | return succeeded; |
| | 2 | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Removes the allocated entry corresponding to the sparse index. |
| | | 418 | | /// </summary> |
| | | 419 | | /// <param name="sparseIndexToRemove">The sparse index corresponding to the entry to remove.</param> |
| | | 420 | | /// <param name="version"> |
| | | 421 | | /// The version number of the int used to access the sparse index. Used to guard against erroneously accessi |
| | | 422 | | /// freed indices currently in use with an outdated reference. |
| | | 423 | | /// </param> |
| | | 424 | | /// <param name="dataIndexToSwapTo">Replace the data array value at this index with the data array value at inde |
| | | 425 | | /// <param name="dataIndexToSwapFrom"> |
| | | 426 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 427 | | /// value at dataIndexToSwapTo. |
| | | 428 | | /// </param> |
| | | 429 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | | 430 | | /// <returns>True if the entry was valid and thus removed.</returns> |
| | | 431 | | public bool RemoveWithBoundsAndVersionChecks(ref int sparseIndexToRemove, ulong version, |
| | | 432 | | ulong[] versionArray, out int dataIndexToSwapFrom, out int dataIndexToSwapTo) |
| | 6 | 433 | | { |
| | 6 | 434 | | dataIndexToSwapFrom = -1; |
| | 6 | 435 | | dataIndexToSwapTo = -1; |
| | 6 | 436 | | bool didRemove = false; |
| | 6 | 437 | | if (sparseIndexToRemove >= 0 && sparseIndexToRemove < SparseArray.Length) |
| | 3 | 438 | | { |
| | 3 | 439 | | ulong sparseIndexVersion = versionArray[sparseIndexToRemove]; |
| | 3 | 440 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | | 441 | | |
| | 3 | 442 | | if (sparseIndexVersion == version && denseIndexToRemove >= 0 && denseIndexToRemove < Count) |
| | 2 | 443 | | { |
| | 2 | 444 | | int sparseIndexAtDenseIndex = DenseArray[denseIndexToRemove]; |
| | 2 | 445 | | int newLength = Count - 1; |
| | 2 | 446 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 447 | | |
| | 2 | 448 | | if (denseIndexToRemove < Count && sparseIndexAtDenseIndex == sparseIndexToRemove) |
| | 2 | 449 | | { |
| | 2 | 450 | | didRemove = true; |
| | 2 | 451 | | versionArray[sparseIndexToRemove] = sparseIndexVersion + 1; |
| | | 452 | | // Swap the entry being removed with the last entry. |
| | 2 | 453 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 2 | 454 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 455 | | |
| | 2 | 456 | | dataIndexToSwapFrom = newLength; |
| | 2 | 457 | | dataIndexToSwapTo = denseIndexToRemove; |
| | | 458 | | |
| | | 459 | | // Clear the dense index, for debugging purposes |
| | 2 | 460 | | DenseArray[newLength] = -1; |
| | | 461 | | |
| | | 462 | | // Add the sparse index to the free list. |
| | 2 | 463 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 2 | 464 | | FreeIndex = sparseIndexToRemove; |
| | | 465 | | |
| | 2 | 466 | | Count = newLength; |
| | 2 | 467 | | } |
| | 2 | 468 | | } |
| | 3 | 469 | | } |
| | | 470 | | |
| | 6 | 471 | | sparseIndexToRemove = -1; |
| | | 472 | | |
| | 6 | 473 | | return didRemove; |
| | 6 | 474 | | } |
| | | 475 | | |
| | | 476 | | /// <summary> |
| | | 477 | | /// Removes the associated sparse/dense index pair from active use. |
| | | 478 | | /// </summary> |
| | | 479 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | | 480 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 481 | | public void RemoveUnchecked(int sparseIndexToRemove) |
| | 1 | 482 | | { |
| | 1 | 483 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 1 | 484 | | int newLength = Count - 1; |
| | 1 | 485 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 486 | | |
| | | 487 | | // Swap the entry being removed with the last entry. |
| | 1 | 488 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 489 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 490 | | |
| | | 491 | | // Clear the dense index, for debugging purposes |
| | 1 | 492 | | DenseArray[newLength] = -1; |
| | | 493 | | |
| | | 494 | | // Add the sparse index to the free list. |
| | 1 | 495 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 496 | | FreeIndex = sparseIndexToRemove; |
| | | 497 | | |
| | 1 | 498 | | Count = newLength; |
| | 1 | 499 | | } |
| | | 500 | | |
| | | 501 | | /// <summary> |
| | | 502 | | /// Removes the associated sparse/dense index pair from active use. |
| | | 503 | | /// Out parameters used to manage parallel data arrays. |
| | | 504 | | /// </summary> |
| | | 505 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | | 506 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | | 507 | | /// <param name="indexToSwapFrom"> |
| | | 508 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 509 | | /// value at indexToSwapTo. |
| | | 510 | | /// </param> |
| | | 511 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 512 | | public void RemoveUnchecked(int sparseIndexToRemove, out int indexToSwapFrom, out int indexToSwapTo) |
| | 1 | 513 | | { |
| | 1 | 514 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 1 | 515 | | int newLength = Count - 1; |
| | 1 | 516 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 517 | | |
| | | 518 | | // Swap the entry being removed with the last entry. |
| | 1 | 519 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 520 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 521 | | |
| | | 522 | | // Clear the dense index, for debugging purposes |
| | 1 | 523 | | DenseArray[newLength] = -1; |
| | | 524 | | |
| | | 525 | | // Add the sparse index to the free list. |
| | 1 | 526 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 527 | | FreeIndex = sparseIndexToRemove; |
| | | 528 | | |
| | 1 | 529 | | Count = newLength; |
| | | 530 | | |
| | 1 | 531 | | indexToSwapTo = denseIndexToRemove; |
| | 1 | 532 | | indexToSwapFrom = newLength; |
| | 1 | 533 | | } |
| | | 534 | | |
| | | 535 | | /// <summary> |
| | | 536 | | /// Removes the associated sparse/dense index pair from active use and increments the version. |
| | | 537 | | /// Out parameters used to manage parallel data arrays. |
| | | 538 | | /// </summary> |
| | | 539 | | /// <param name="sparseIndexToRemove">The sparse index to remove.</param> |
| | | 540 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | | 541 | | /// <param name="indexToSwapTo">Replace the data array value at this index with the data array value at indexToS |
| | | 542 | | /// <param name="indexToSwapFrom"> |
| | | 543 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 544 | | /// value at indexToSwapTo. |
| | | 545 | | /// </param> |
| | | 546 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 547 | | public void RemoveUnchecked(int sparseIndexToRemove, ulong[] versionArray, out int indexToSwapFrom, out int inde |
| | 2 | 548 | | { |
| | 2 | 549 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 2 | 550 | | int newLength = Count - 1; |
| | 2 | 551 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 552 | | |
| | | 553 | | // Swap the entry being removed with the last entry. |
| | 2 | 554 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 2 | 555 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 556 | | |
| | | 557 | | // Clear the dense index, for debugging purposes |
| | 2 | 558 | | DenseArray[newLength] = -1; |
| | | 559 | | |
| | | 560 | | // Add the sparse index to the free list. |
| | 2 | 561 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 2 | 562 | | versionArray[sparseIndexToRemove] = versionArray[sparseIndexToRemove] + 1; |
| | 2 | 563 | | FreeIndex = sparseIndexToRemove; |
| | | 564 | | |
| | 2 | 565 | | Count = newLength; |
| | | 566 | | |
| | 2 | 567 | | indexToSwapTo = denseIndexToRemove; |
| | 2 | 568 | | indexToSwapFrom = newLength; |
| | 2 | 569 | | } |
| | | 570 | | |
| | | 571 | | /// <summary> |
| | | 572 | | /// Removes the associated sparse/dense index pair from active use. |
| | | 573 | | /// </summary> |
| | | 574 | | /// <param name="denseIndexToRemove">The dense index associated with the sparse index to remove.</param> |
| | | 575 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove) |
| | 1 | 576 | | { |
| | 1 | 577 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| | 1 | 578 | | int newLength = Count - 1; |
| | 1 | 579 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 580 | | |
| | | 581 | | // Swap the entry being removed with the last entry. |
| | 1 | 582 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 583 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 584 | | |
| | | 585 | | // Clear the dense index, for debugging purposes |
| | 1 | 586 | | DenseArray[newLength] = -1; |
| | | 587 | | |
| | | 588 | | // Add the sparse index to the free list. |
| | 1 | 589 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 590 | | FreeIndex = sparseIndexToRemove; |
| | | 591 | | |
| | 1 | 592 | | Count = newLength; |
| | 1 | 593 | | } |
| | | 594 | | |
| | | 595 | | /// <summary> |
| | | 596 | | /// Removes the associated sparse/dense index pair from active use. |
| | | 597 | | /// </summary> |
| | | 598 | | /// <param name="denseIndexToRemove">The dense index associated with the sparse index to remove.</param> |
| | | 599 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | | 600 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, ulong[] versionArray) |
| | 1 | 601 | | { |
| | 1 | 602 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| | 1 | 603 | | int newLength = Count - 1; |
| | 1 | 604 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 605 | | |
| | | 606 | | // Swap the entry being removed with the last entry. |
| | 1 | 607 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 608 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 609 | | |
| | | 610 | | // Clear the dense index, for debugging purposes |
| | 1 | 611 | | DenseArray[newLength] = -1; |
| | | 612 | | |
| | | 613 | | // Add the sparse index to the free list. |
| | 1 | 614 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 615 | | versionArray[sparseIndexToRemove] += 1; |
| | 1 | 616 | | FreeIndex = sparseIndexToRemove; |
| | | 617 | | |
| | 1 | 618 | | Count = newLength; |
| | 1 | 619 | | } |
| | | 620 | | |
| | | 621 | | /// <summary> |
| | | 622 | | /// Removes the associated sparse/dense index pair from active use. |
| | | 623 | | /// Out parameter used to manage parallel data arrays. |
| | | 624 | | /// </summary> |
| | | 625 | | /// <param name="denseIndexToRemove">The sparse index to remove.</param> |
| | | 626 | | /// <param name="indexToSwapFrom"> |
| | | 627 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 628 | | /// value at denseIndexToRemove. |
| | | 629 | | /// </param> |
| | | 630 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, out int indexToSwapFrom) |
| | 1 | 631 | | { |
| | 1 | 632 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| | 1 | 633 | | int newLength = Count - 1; |
| | 1 | 634 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 635 | | |
| | | 636 | | // Swap the entry being removed with the last entry. |
| | 1 | 637 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 638 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 639 | | |
| | | 640 | | // Clear the dense index, for debugging purposes |
| | 1 | 641 | | DenseArray[newLength] = -1; |
| | | 642 | | |
| | | 643 | | // Add the sparse index to the free list. |
| | 1 | 644 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 645 | | FreeIndex = sparseIndexToRemove; |
| | | 646 | | |
| | 1 | 647 | | Count = newLength; |
| | | 648 | | |
| | 1 | 649 | | indexToSwapFrom = newLength; |
| | 1 | 650 | | } |
| | | 651 | | |
| | | 652 | | /// <summary> |
| | | 653 | | /// Removes the associated sparse/dense index pair from active use. |
| | | 654 | | /// Out parameter used to manage parallel data arrays. |
| | | 655 | | /// </summary> |
| | | 656 | | /// <param name="denseIndexToRemove">The sparse index to remove.</param> |
| | | 657 | | /// <param name="versionArray">The array where version numbers to check against are stored.</param> |
| | | 658 | | /// <param name="indexToSwapFrom"> |
| | | 659 | | /// Set the data array value at this index to default after swapping with the data array |
| | | 660 | | /// value at denseIndexToRemove. |
| | | 661 | | /// </param> |
| | | 662 | | public void RemoveUncheckedFromDenseIndex(int denseIndexToRemove, ulong[] versionArray, out int indexToSwapFrom) |
| | 1 | 663 | | { |
| | 1 | 664 | | int sparseIndexToRemove = DenseArray[denseIndexToRemove]; |
| | 1 | 665 | | int newLength = Count - 1; |
| | 1 | 666 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 667 | | |
| | | 668 | | // Swap the entry being removed with the last entry. |
| | 1 | 669 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 1 | 670 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | | 671 | | |
| | | 672 | | // Clear the dense index, for debugging purposes |
| | 1 | 673 | | DenseArray[newLength] = -1; |
| | | 674 | | |
| | | 675 | | // Add the sparse index to the free list. |
| | 1 | 676 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 1 | 677 | | versionArray[sparseIndexToRemove] = versionArray[sparseIndexToRemove] + 1; |
| | 1 | 678 | | FreeIndex = sparseIndexToRemove; |
| | | 679 | | |
| | 1 | 680 | | Count = newLength; |
| | | 681 | | |
| | 1 | 682 | | indexToSwapFrom = newLength; |
| | 1 | 683 | | } |
| | | 684 | | |
| | | 685 | | /// <summary> |
| | | 686 | | /// Clear the dense and sparse arrays. |
| | | 687 | | /// </summary> |
| | | 688 | | public void Clear() |
| | 1 | 689 | | { |
| | 1 | 690 | | int capacity = SparseArray.Length; |
| | 8 | 691 | | for (int i = 0; i < capacity; i++) |
| | 3 | 692 | | { |
| | 3 | 693 | | DenseArray[i] = -1; |
| | 3 | 694 | | SparseArray[i] = i + 1; |
| | 3 | 695 | | } |
| | | 696 | | |
| | 1 | 697 | | FreeIndex = 0; |
| | 1 | 698 | | Count = 0; |
| | 1 | 699 | | } |
| | | 700 | | |
| | | 701 | | /// <summary> |
| | | 702 | | /// Clear the dense and sparse arrays. |
| | | 703 | | /// </summary> |
| | | 704 | | /// /// |
| | | 705 | | /// <param name="versionArray">Array containing version numbers to check against.</param> |
| | | 706 | | public void Clear(ulong[] versionArray) |
| | 1 | 707 | | { |
| | 1 | 708 | | int capacity = SparseArray.Length; |
| | 8 | 709 | | for (int i = 0; i < capacity; i++) |
| | 3 | 710 | | { |
| | 3 | 711 | | DenseArray[i] = -1; |
| | 3 | 712 | | SparseArray[i] = i + 1; |
| | 3 | 713 | | versionArray[i] = versionArray[i] + 1; |
| | 3 | 714 | | } |
| | | 715 | | |
| | 1 | 716 | | FreeIndex = 0; |
| | 1 | 717 | | Count = 0; |
| | 1 | 718 | | } |
| | | 719 | | |
| | | 720 | | /// <summary> |
| | | 721 | | /// Clear the dense and sparse arrays and reset the version array. |
| | | 722 | | /// Note: Only clear the version array if you are sure there are no outstanding dependencies on version numb |
| | | 723 | | /// </summary> |
| | | 724 | | /// /// |
| | | 725 | | /// <param name="versionArray">Array containing version numbers to check against.</param> |
| | | 726 | | public void ClearWithVersionArrayReset(ulong[] versionArray) |
| | 1 | 727 | | { |
| | 1 | 728 | | int capacity = SparseArray.Length; |
| | 8 | 729 | | for (int i = 0; i < capacity; i++) |
| | 3 | 730 | | { |
| | 3 | 731 | | DenseArray[i] = -1; |
| | 3 | 732 | | SparseArray[i] = i + 1; |
| | 3 | 733 | | versionArray[i] = 1; |
| | 3 | 734 | | } |
| | | 735 | | |
| | 1 | 736 | | FreeIndex = 0; |
| | 1 | 737 | | Count = 0; |
| | 1 | 738 | | } |
| | | 739 | | |
| | | 740 | | /// <summary> |
| | | 741 | | /// Reallocate the dense and sparse arrays with additional capacity. |
| | | 742 | | /// </summary> |
| | | 743 | | /// <param name="extraCapacity">How many indices to expand the dense and sparse arrays by.</param> |
| | | 744 | | public void Expand(int extraCapacity) |
| | 1 | 745 | | { |
| | 1 | 746 | | int currentCapacity = SparseArray.Length; |
| | 1 | 747 | | int newCapacity = currentCapacity + extraCapacity; |
| | | 748 | | |
| | 1 | 749 | | int[] newSparseArray = new int[newCapacity]; |
| | 1 | 750 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 1 | 751 | | SparseArray = newSparseArray; |
| | | 752 | | |
| | 1 | 753 | | int[] newDenseArray = new int[newCapacity]; |
| | 1 | 754 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 1 | 755 | | DenseArray = newDenseArray; |
| | | 756 | | |
| | 8 | 757 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 3 | 758 | | { |
| | 3 | 759 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 3 | 760 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 3 | 761 | | } |
| | 1 | 762 | | } |
| | | 763 | | |
| | | 764 | | /// <summary> |
| | | 765 | | /// Reallocate the dense and sparse arrays with additional capacity. |
| | | 766 | | /// </summary> |
| | | 767 | | /// <param name="extraCapacity">How many indices to expand the dense and sparse arrays by.</param> |
| | | 768 | | /// <param name="versionArray">Array containing version numbers to check against.</param> |
| | | 769 | | public void Expand(int extraCapacity, ref ulong[] versionArray) |
| | 2 | 770 | | { |
| | 2 | 771 | | int currentCapacity = SparseArray.Length; |
| | 2 | 772 | | int newCapacity = currentCapacity + extraCapacity; |
| | | 773 | | |
| | 2 | 774 | | int[] newSparseArray = new int[newCapacity]; |
| | 2 | 775 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 2 | 776 | | SparseArray = newSparseArray; |
| | | 777 | | |
| | 2 | 778 | | int[] newDenseArray = new int[newCapacity]; |
| | 2 | 779 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 2 | 780 | | DenseArray = newDenseArray; |
| | | 781 | | |
| | 2 | 782 | | ulong[] newVersionArray = new ulong[newCapacity]; |
| | 2 | 783 | | Array.Copy(versionArray, 0, newVersionArray, 0, currentCapacity); |
| | 2 | 784 | | versionArray = newVersionArray; |
| | | 785 | | |
| | 16 | 786 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 6 | 787 | | { |
| | 6 | 788 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 6 | 789 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 6 | 790 | | versionArray[i] = 1; |
| | 6 | 791 | | } |
| | 2 | 792 | | } |
| | | 793 | | |
| | | 794 | | public void Reserve(int numberToReserve) |
| | 1 | 795 | | { |
| | 1 | 796 | | int currentCapacity = SparseArray.Length; |
| | 1 | 797 | | int currentCount = Count; |
| | 1 | 798 | | int newCount = currentCount + numberToReserve; |
| | | 799 | | |
| | 1 | 800 | | if (newCount > currentCapacity) |
| | 1 | 801 | | { |
| | 1 | 802 | | int[] newSparseArray = new int[newCount]; |
| | 1 | 803 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 1 | 804 | | SparseArray = newSparseArray; |
| | | 805 | | |
| | 1 | 806 | | int[] newDenseArray = new int[newCount]; |
| | 1 | 807 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 1 | 808 | | DenseArray = newDenseArray; |
| | | 809 | | |
| | 4 | 810 | | for (int i = currentCapacity; i < newCount; i++) |
| | 1 | 811 | | { |
| | 1 | 812 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 1 | 813 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 1 | 814 | | } |
| | 1 | 815 | | } |
| | 1 | 816 | | } |
| | | 817 | | |
| | | 818 | | public void Reserve(int numberToReserve, ref ulong[] versionArray) |
| | 0 | 819 | | { |
| | 0 | 820 | | int currentCapacity = SparseArray.Length; |
| | 0 | 821 | | int currentCount = Count; |
| | 0 | 822 | | int newCount = currentCount + numberToReserve; |
| | | 823 | | |
| | 0 | 824 | | if (newCount > currentCapacity) |
| | 0 | 825 | | { |
| | 0 | 826 | | int[] newSparseArray = new int[newCount]; |
| | 0 | 827 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 828 | | SparseArray = newSparseArray; |
| | | 829 | | |
| | 0 | 830 | | int[] newDenseArray = new int[newCount]; |
| | 0 | 831 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 832 | | DenseArray = newDenseArray; |
| | | 833 | | |
| | 0 | 834 | | ulong[] newVersionArray = new ulong[newCount]; |
| | 0 | 835 | | Array.Copy(versionArray, 0, newVersionArray, 0, currentCapacity); |
| | 0 | 836 | | versionArray = newVersionArray; |
| | | 837 | | |
| | 0 | 838 | | for (int i = currentCapacity; i < newCount; i++) |
| | 0 | 839 | | { |
| | 0 | 840 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 841 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 842 | | versionArray[i] = 1; |
| | 0 | 843 | | } |
| | 0 | 844 | | } |
| | 0 | 845 | | } |
| | | 846 | | |
| | | 847 | | #region Parallel array method duplicates |
| | | 848 | | |
| | | 849 | | /// <summary> |
| | | 850 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 851 | | /// </summary> |
| | | 852 | | /// <returns>True if the index pool expanded.</returns> |
| | | 853 | | public bool AddWithExpandCheck<T0>(T0 obj0, ref T0[] array0, out int lookupIndex, int howMuchToExpand = 16) |
| | 0 | 854 | | { |
| | 0 | 855 | | int indexToClaim = FreeIndex; |
| | 0 | 856 | | int currentCapacity = SparseArray.Length; |
| | 0 | 857 | | bool needsExpansion = false; |
| | | 858 | | |
| | 0 | 859 | | if (indexToClaim >= currentCapacity) |
| | 0 | 860 | | { |
| | 0 | 861 | | needsExpansion = true; |
| | | 862 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 863 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 864 | | |
| | 0 | 865 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 866 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 867 | | SparseArray = newSparseArray; |
| | | 868 | | |
| | 0 | 869 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 870 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 871 | | DenseArray = newDenseArray; |
| | | 872 | | |
| | 0 | 873 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 874 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 875 | | array0 = newArray0; |
| | | 876 | | |
| | 0 | 877 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 878 | | { |
| | 0 | 879 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 880 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 881 | | } |
| | 0 | 882 | | } |
| | | 883 | | |
| | 0 | 884 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 885 | | |
| | 0 | 886 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 887 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 888 | | |
| | 0 | 889 | | array0[Count] = obj0; |
| | | 890 | | |
| | 0 | 891 | | ++Count; |
| | 0 | 892 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 893 | | |
| | 0 | 894 | | lookupIndex = indexToClaim; |
| | 0 | 895 | | return needsExpansion; |
| | 0 | 896 | | } |
| | | 897 | | |
| | | 898 | | /// <summary> |
| | | 899 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 900 | | /// </summary> |
| | | 901 | | /// <returns>True if the index pool expanded.</returns> |
| | | 902 | | public bool AddWithExpandCheck<T0, T1>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, out int lookupIndex, |
| | | 903 | | int howMuchToExpand = 16) |
| | 0 | 904 | | { |
| | 0 | 905 | | int indexToClaim = FreeIndex; |
| | 0 | 906 | | int currentCapacity = SparseArray.Length; |
| | 0 | 907 | | bool needsExpansion = false; |
| | | 908 | | |
| | 0 | 909 | | if (indexToClaim >= currentCapacity) |
| | 0 | 910 | | { |
| | 0 | 911 | | needsExpansion = true; |
| | | 912 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 913 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 914 | | |
| | 0 | 915 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 916 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 917 | | SparseArray = newSparseArray; |
| | | 918 | | |
| | 0 | 919 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 920 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 921 | | DenseArray = newDenseArray; |
| | | 922 | | |
| | 0 | 923 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 924 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 925 | | array0 = newArray0; |
| | | 926 | | |
| | 0 | 927 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 928 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 929 | | array1 = newArray1; |
| | | 930 | | |
| | 0 | 931 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 932 | | { |
| | 0 | 933 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 934 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 935 | | } |
| | 0 | 936 | | } |
| | | 937 | | |
| | 0 | 938 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 939 | | |
| | 0 | 940 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 941 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 942 | | |
| | 0 | 943 | | array0[Count] = obj0; |
| | 0 | 944 | | array1[Count] = obj1; |
| | | 945 | | |
| | 0 | 946 | | ++Count; |
| | 0 | 947 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 948 | | |
| | 0 | 949 | | lookupIndex = indexToClaim; |
| | 0 | 950 | | return needsExpansion; |
| | 0 | 951 | | } |
| | | 952 | | |
| | | 953 | | /// <summary> |
| | | 954 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 955 | | /// </summary> |
| | | 956 | | /// <returns>True if the index pool expanded.</returns> |
| | | 957 | | public bool AddWithExpandCheck<T0, T1, T2>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, T2 obj2, |
| | | 958 | | ref T2[] array2, |
| | | 959 | | out int lookupIndex, int howMuchToExpand = 16) |
| | 0 | 960 | | { |
| | 0 | 961 | | int indexToClaim = FreeIndex; |
| | 0 | 962 | | int currentCapacity = SparseArray.Length; |
| | 0 | 963 | | bool needsExpansion = false; |
| | | 964 | | |
| | 0 | 965 | | if (indexToClaim >= currentCapacity) |
| | 0 | 966 | | { |
| | 0 | 967 | | needsExpansion = true; |
| | | 968 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 969 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 970 | | |
| | 0 | 971 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 972 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 973 | | SparseArray = newSparseArray; |
| | | 974 | | |
| | 0 | 975 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 976 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 977 | | DenseArray = newDenseArray; |
| | | 978 | | |
| | 0 | 979 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 980 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 981 | | array0 = newArray0; |
| | | 982 | | |
| | 0 | 983 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 984 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 985 | | array1 = newArray1; |
| | | 986 | | |
| | 0 | 987 | | T2[] newArray2 = new T2[newCapacity]; |
| | 0 | 988 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| | 0 | 989 | | array2 = newArray2; |
| | | 990 | | |
| | 0 | 991 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 992 | | { |
| | 0 | 993 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 994 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 995 | | } |
| | 0 | 996 | | } |
| | | 997 | | |
| | 0 | 998 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 999 | | |
| | 0 | 1000 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1001 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1002 | | |
| | 0 | 1003 | | array0[Count] = obj0; |
| | 0 | 1004 | | array1[Count] = obj1; |
| | 0 | 1005 | | array2[Count] = obj2; |
| | | 1006 | | |
| | 0 | 1007 | | ++Count; |
| | 0 | 1008 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1009 | | |
| | 0 | 1010 | | lookupIndex = indexToClaim; |
| | 0 | 1011 | | return needsExpansion; |
| | 0 | 1012 | | } |
| | | 1013 | | |
| | | 1014 | | /// <summary> |
| | | 1015 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 1016 | | /// </summary> |
| | | 1017 | | /// <returns>True if the index pool expanded.</returns> |
| | | 1018 | | public bool AddWithExpandCheck<T0, T1, T2, T3>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, T2 obj2, |
| | | 1019 | | ref T2[] array2, T3 obj3, ref T3[] array3, out int lookupIndex, int howMuchToExpand = 16) |
| | 0 | 1020 | | { |
| | 0 | 1021 | | int indexToClaim = FreeIndex; |
| | 0 | 1022 | | int currentCapacity = SparseArray.Length; |
| | 0 | 1023 | | bool needsExpansion = false; |
| | | 1024 | | |
| | 0 | 1025 | | if (indexToClaim >= currentCapacity) |
| | 0 | 1026 | | { |
| | 0 | 1027 | | needsExpansion = true; |
| | | 1028 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 1029 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 1030 | | |
| | 0 | 1031 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 1032 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 1033 | | SparseArray = newSparseArray; |
| | | 1034 | | |
| | 0 | 1035 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 1036 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 1037 | | DenseArray = newDenseArray; |
| | | 1038 | | |
| | 0 | 1039 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 1040 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 1041 | | array0 = newArray0; |
| | | 1042 | | |
| | 0 | 1043 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 1044 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 1045 | | array1 = newArray1; |
| | | 1046 | | |
| | 0 | 1047 | | T2[] newArray2 = new T2[newCapacity]; |
| | 0 | 1048 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| | 0 | 1049 | | array2 = newArray2; |
| | | 1050 | | |
| | 0 | 1051 | | T3[] newArray3 = new T3[newCapacity]; |
| | 0 | 1052 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| | 0 | 1053 | | array3 = newArray3; |
| | | 1054 | | |
| | 0 | 1055 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 1056 | | { |
| | 0 | 1057 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 1058 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 1059 | | } |
| | 0 | 1060 | | } |
| | | 1061 | | |
| | 0 | 1062 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 1063 | | |
| | 0 | 1064 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1065 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1066 | | |
| | 0 | 1067 | | array0[Count] = obj0; |
| | 0 | 1068 | | array1[Count] = obj1; |
| | 0 | 1069 | | array2[Count] = obj2; |
| | 0 | 1070 | | array3[Count] = obj3; |
| | | 1071 | | |
| | 0 | 1072 | | ++Count; |
| | 0 | 1073 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1074 | | |
| | 0 | 1075 | | lookupIndex = indexToClaim; |
| | 0 | 1076 | | return needsExpansion; |
| | 0 | 1077 | | } |
| | | 1078 | | |
| | | 1079 | | /// <summary> |
| | | 1080 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 1081 | | /// </summary> |
| | | 1082 | | /// <returns>True if the index pool expanded.</returns> |
| | | 1083 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, T2 obj2, |
| | | 1084 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, out int lookupIndex, |
| | | 1085 | | int howMuchToExpand = 16) |
| | 0 | 1086 | | { |
| | 0 | 1087 | | int indexToClaim = FreeIndex; |
| | 0 | 1088 | | int currentCapacity = SparseArray.Length; |
| | 0 | 1089 | | bool needsExpansion = false; |
| | | 1090 | | |
| | 0 | 1091 | | if (indexToClaim >= currentCapacity) |
| | 0 | 1092 | | { |
| | 0 | 1093 | | needsExpansion = true; |
| | | 1094 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 1095 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 1096 | | |
| | 0 | 1097 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 1098 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 1099 | | SparseArray = newSparseArray; |
| | | 1100 | | |
| | 0 | 1101 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 1102 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 1103 | | DenseArray = newDenseArray; |
| | | 1104 | | |
| | 0 | 1105 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 1106 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 1107 | | array0 = newArray0; |
| | | 1108 | | |
| | 0 | 1109 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 1110 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 1111 | | array1 = newArray1; |
| | | 1112 | | |
| | 0 | 1113 | | T2[] newArray2 = new T2[newCapacity]; |
| | 0 | 1114 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| | 0 | 1115 | | array2 = newArray2; |
| | | 1116 | | |
| | 0 | 1117 | | T3[] newArray3 = new T3[newCapacity]; |
| | 0 | 1118 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| | 0 | 1119 | | array3 = newArray3; |
| | | 1120 | | |
| | 0 | 1121 | | T4[] newArray4 = new T4[newCapacity]; |
| | 0 | 1122 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| | 0 | 1123 | | array4 = newArray4; |
| | | 1124 | | |
| | 0 | 1125 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 1126 | | { |
| | 0 | 1127 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 1128 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 1129 | | } |
| | 0 | 1130 | | } |
| | | 1131 | | |
| | 0 | 1132 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 1133 | | |
| | 0 | 1134 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1135 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1136 | | |
| | 0 | 1137 | | array0[Count] = obj0; |
| | 0 | 1138 | | array1[Count] = obj1; |
| | 0 | 1139 | | array2[Count] = obj2; |
| | 0 | 1140 | | array3[Count] = obj3; |
| | 0 | 1141 | | array4[Count] = obj4; |
| | | 1142 | | |
| | 0 | 1143 | | ++Count; |
| | 0 | 1144 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1145 | | |
| | 0 | 1146 | | lookupIndex = indexToClaim; |
| | 0 | 1147 | | return needsExpansion; |
| | 0 | 1148 | | } |
| | | 1149 | | |
| | | 1150 | | /// <summary> |
| | | 1151 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 1152 | | /// </summary> |
| | | 1153 | | /// <returns>True if the index pool expanded.</returns> |
| | | 1154 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4, T5>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, |
| | | 1155 | | T2 obj2, |
| | | 1156 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, T5 obj5, ref T5[] array5, |
| | | 1157 | | out int lookupIndex, int howMuchToExpand = 16) |
| | 0 | 1158 | | { |
| | 0 | 1159 | | int indexToClaim = FreeIndex; |
| | 0 | 1160 | | int currentCapacity = SparseArray.Length; |
| | 0 | 1161 | | bool needsExpansion = false; |
| | | 1162 | | |
| | 0 | 1163 | | if (indexToClaim >= currentCapacity) |
| | 0 | 1164 | | { |
| | 0 | 1165 | | needsExpansion = true; |
| | | 1166 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 1167 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 1168 | | |
| | 0 | 1169 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 1170 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 1171 | | SparseArray = newSparseArray; |
| | | 1172 | | |
| | 0 | 1173 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 1174 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 1175 | | DenseArray = newDenseArray; |
| | | 1176 | | |
| | 0 | 1177 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 1178 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 1179 | | array0 = newArray0; |
| | | 1180 | | |
| | 0 | 1181 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 1182 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 1183 | | array1 = newArray1; |
| | | 1184 | | |
| | 0 | 1185 | | T2[] newArray2 = new T2[newCapacity]; |
| | 0 | 1186 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| | 0 | 1187 | | array2 = newArray2; |
| | | 1188 | | |
| | 0 | 1189 | | T3[] newArray3 = new T3[newCapacity]; |
| | 0 | 1190 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| | 0 | 1191 | | array3 = newArray3; |
| | | 1192 | | |
| | 0 | 1193 | | T4[] newArray4 = new T4[newCapacity]; |
| | 0 | 1194 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| | 0 | 1195 | | array4 = newArray4; |
| | | 1196 | | |
| | 0 | 1197 | | T5[] newArray5 = new T5[newCapacity]; |
| | 0 | 1198 | | Array.Copy(array5, 0, newArray5, 0, currentCapacity); |
| | 0 | 1199 | | array5 = newArray5; |
| | | 1200 | | |
| | 0 | 1201 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 1202 | | { |
| | 0 | 1203 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 1204 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 1205 | | } |
| | 0 | 1206 | | } |
| | | 1207 | | |
| | 0 | 1208 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 1209 | | |
| | 0 | 1210 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1211 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1212 | | |
| | 0 | 1213 | | array0[Count] = obj0; |
| | 0 | 1214 | | array1[Count] = obj1; |
| | 0 | 1215 | | array2[Count] = obj2; |
| | 0 | 1216 | | array3[Count] = obj3; |
| | 0 | 1217 | | array4[Count] = obj4; |
| | 0 | 1218 | | array5[Count] = obj5; |
| | | 1219 | | |
| | 0 | 1220 | | ++Count; |
| | 0 | 1221 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1222 | | |
| | 0 | 1223 | | lookupIndex = indexToClaim; |
| | 0 | 1224 | | return needsExpansion; |
| | 0 | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | /// <summary> |
| | | 1228 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 1229 | | /// </summary> |
| | | 1230 | | /// <returns>True if the index pool expanded.</returns> |
| | | 1231 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4, T5, T6>(T0 obj0, ref T0[] array0, T1 obj1, ref T1[] array1, |
| | | 1232 | | T2 obj2, |
| | | 1233 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, T5 obj5, ref T5[] array5, T6 obj6, |
| | | 1234 | | ref T6[] array6, out int lookupIndex, int howMuchToExpand = 16) |
| | 0 | 1235 | | { |
| | 0 | 1236 | | int indexToClaim = FreeIndex; |
| | 0 | 1237 | | int currentCapacity = SparseArray.Length; |
| | 0 | 1238 | | bool needsExpansion = false; |
| | | 1239 | | |
| | 0 | 1240 | | if (indexToClaim >= currentCapacity) |
| | 0 | 1241 | | { |
| | 0 | 1242 | | needsExpansion = true; |
| | | 1243 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 1244 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 1245 | | |
| | 0 | 1246 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 1247 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 1248 | | SparseArray = newSparseArray; |
| | | 1249 | | |
| | 0 | 1250 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 1251 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 1252 | | DenseArray = newDenseArray; |
| | | 1253 | | |
| | 0 | 1254 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 1255 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 1256 | | array0 = newArray0; |
| | | 1257 | | |
| | 0 | 1258 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 1259 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 1260 | | array1 = newArray1; |
| | | 1261 | | |
| | 0 | 1262 | | T2[] newArray2 = new T2[newCapacity]; |
| | 0 | 1263 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| | 0 | 1264 | | array2 = newArray2; |
| | | 1265 | | |
| | 0 | 1266 | | T3[] newArray3 = new T3[newCapacity]; |
| | 0 | 1267 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| | 0 | 1268 | | array3 = newArray3; |
| | | 1269 | | |
| | 0 | 1270 | | T4[] newArray4 = new T4[newCapacity]; |
| | 0 | 1271 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| | 0 | 1272 | | array4 = newArray4; |
| | | 1273 | | |
| | 0 | 1274 | | T5[] newArray5 = new T5[newCapacity]; |
| | 0 | 1275 | | Array.Copy(array5, 0, newArray5, 0, currentCapacity); |
| | 0 | 1276 | | array5 = newArray5; |
| | | 1277 | | |
| | 0 | 1278 | | T6[] newArray6 = new T6[newCapacity]; |
| | 0 | 1279 | | Array.Copy(array6, 0, newArray6, 0, currentCapacity); |
| | 0 | 1280 | | array6 = newArray6; |
| | | 1281 | | |
| | 0 | 1282 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 1283 | | { |
| | 0 | 1284 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 1285 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 1286 | | } |
| | 0 | 1287 | | } |
| | | 1288 | | |
| | 0 | 1289 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 1290 | | |
| | 0 | 1291 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1292 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1293 | | |
| | 0 | 1294 | | array0[Count] = obj0; |
| | 0 | 1295 | | array1[Count] = obj1; |
| | 0 | 1296 | | array2[Count] = obj2; |
| | 0 | 1297 | | array3[Count] = obj3; |
| | 0 | 1298 | | array4[Count] = obj4; |
| | 0 | 1299 | | array5[Count] = obj5; |
| | 0 | 1300 | | array6[Count] = obj6; |
| | | 1301 | | |
| | 0 | 1302 | | ++Count; |
| | 0 | 1303 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1304 | | |
| | 0 | 1305 | | lookupIndex = indexToClaim; |
| | 0 | 1306 | | return needsExpansion; |
| | 0 | 1307 | | } |
| | | 1308 | | |
| | | 1309 | | /// <summary> |
| | | 1310 | | /// Adds a sparse/dense index pair to the set and expands the arrays if necessary. |
| | | 1311 | | /// </summary> |
| | | 1312 | | /// <returns>True if the index pool expanded.</returns> |
| | | 1313 | | public bool AddWithExpandCheck<T0, T1, T2, T3, T4, T5, T6, T7>(T0 obj0, ref T0[] array0, T1 obj1, |
| | | 1314 | | ref T1[] array1, T2 obj2, |
| | | 1315 | | ref T2[] array2, T3 obj3, ref T3[] array3, T4 obj4, ref T4[] array4, T5 obj5, ref T5[] array5, T6 obj6, |
| | | 1316 | | ref T6[] array6, T7 obj7, ref T7[] array7, out int lookupIndex, int howMuchToExpand = 16) |
| | 0 | 1317 | | { |
| | 0 | 1318 | | int indexToClaim = FreeIndex; |
| | 0 | 1319 | | int currentCapacity = SparseArray.Length; |
| | 0 | 1320 | | bool needsExpansion = false; |
| | | 1321 | | |
| | 0 | 1322 | | if (indexToClaim >= currentCapacity) |
| | 0 | 1323 | | { |
| | 0 | 1324 | | needsExpansion = true; |
| | | 1325 | | // We're out of space, the last free index points to nothing. Allocate more indices. |
| | 0 | 1326 | | int newCapacity = currentCapacity + howMuchToExpand; |
| | | 1327 | | |
| | 0 | 1328 | | int[] newSparseArray = new int[newCapacity]; |
| | 0 | 1329 | | Array.Copy(SparseArray, 0, newSparseArray, 0, currentCapacity); |
| | 0 | 1330 | | SparseArray = newSparseArray; |
| | | 1331 | | |
| | 0 | 1332 | | int[] newDenseArray = new int[newCapacity]; |
| | 0 | 1333 | | Array.Copy(DenseArray, 0, newDenseArray, 0, currentCapacity); |
| | 0 | 1334 | | DenseArray = newDenseArray; |
| | | 1335 | | |
| | 0 | 1336 | | T0[] newArray0 = new T0[newCapacity]; |
| | 0 | 1337 | | Array.Copy(array0, 0, newArray0, 0, currentCapacity); |
| | 0 | 1338 | | array0 = newArray0; |
| | | 1339 | | |
| | 0 | 1340 | | T1[] newArray1 = new T1[newCapacity]; |
| | 0 | 1341 | | Array.Copy(array1, 0, newArray1, 0, currentCapacity); |
| | 0 | 1342 | | array1 = newArray1; |
| | | 1343 | | |
| | 0 | 1344 | | T2[] newArray2 = new T2[newCapacity]; |
| | 0 | 1345 | | Array.Copy(array2, 0, newArray2, 0, currentCapacity); |
| | 0 | 1346 | | array2 = newArray2; |
| | | 1347 | | |
| | 0 | 1348 | | T3[] newArray3 = new T3[newCapacity]; |
| | 0 | 1349 | | Array.Copy(array3, 0, newArray3, 0, currentCapacity); |
| | 0 | 1350 | | array3 = newArray3; |
| | | 1351 | | |
| | 0 | 1352 | | T4[] newArray4 = new T4[newCapacity]; |
| | 0 | 1353 | | Array.Copy(array4, 0, newArray4, 0, currentCapacity); |
| | 0 | 1354 | | array4 = newArray4; |
| | | 1355 | | |
| | 0 | 1356 | | T5[] newArray5 = new T5[newCapacity]; |
| | 0 | 1357 | | Array.Copy(array5, 0, newArray5, 0, currentCapacity); |
| | 0 | 1358 | | array5 = newArray5; |
| | | 1359 | | |
| | 0 | 1360 | | T6[] newArray6 = new T6[newCapacity]; |
| | 0 | 1361 | | Array.Copy(array6, 0, newArray6, 0, currentCapacity); |
| | 0 | 1362 | | array6 = newArray6; |
| | | 1363 | | |
| | 0 | 1364 | | T7[] newArray7 = new T7[newCapacity]; |
| | 0 | 1365 | | Array.Copy(array7, 0, newArray7, 0, currentCapacity); |
| | 0 | 1366 | | array7 = newArray7; |
| | | 1367 | | |
| | 0 | 1368 | | for (int i = currentCapacity; i < newCapacity; i++) |
| | 0 | 1369 | | { |
| | 0 | 1370 | | SparseArray[i] = i + 1; // Build the free list chain. |
| | 0 | 1371 | | DenseArray[i] = -1; // Set new dense indices as unclaimed. |
| | 0 | 1372 | | } |
| | 0 | 1373 | | } |
| | | 1374 | | |
| | 0 | 1375 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | | 1376 | | |
| | 0 | 1377 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1378 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1379 | | |
| | 0 | 1380 | | array0[Count] = obj0; |
| | 0 | 1381 | | array1[Count] = obj1; |
| | 0 | 1382 | | array2[Count] = obj2; |
| | 0 | 1383 | | array3[Count] = obj3; |
| | 0 | 1384 | | array4[Count] = obj4; |
| | 0 | 1385 | | array5[Count] = obj5; |
| | 0 | 1386 | | array6[Count] = obj6; |
| | 0 | 1387 | | array7[Count] = obj7; |
| | | 1388 | | |
| | 0 | 1389 | | ++Count; |
| | 0 | 1390 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1391 | | |
| | 0 | 1392 | | lookupIndex = indexToClaim; |
| | 0 | 1393 | | return needsExpansion; |
| | 0 | 1394 | | } |
| | | 1395 | | |
| | | 1396 | | /// <summary> |
| | | 1397 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1398 | | /// </summary> |
| | | 1399 | | /// <returns>The sparse index allocated</returns> |
| | | 1400 | | public int AddUnchecked<T0>(T0 obj0, T0[] array0) |
| | 0 | 1401 | | { |
| | 0 | 1402 | | int indexToClaim = FreeIndex; |
| | 0 | 1403 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1404 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1405 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1406 | | |
| | 0 | 1407 | | array0[Count] = obj0; |
| | | 1408 | | |
| | 0 | 1409 | | ++Count; |
| | 0 | 1410 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1411 | | |
| | 0 | 1412 | | return indexToClaim; |
| | 0 | 1413 | | } |
| | | 1414 | | |
| | | 1415 | | /// <summary> |
| | | 1416 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1417 | | /// </summary> |
| | | 1418 | | /// <returns>The sparse index allocated</returns> |
| | | 1419 | | public int AddUnchecked<T0, T1>(T0 obj0, T0[] array0, T1 obj1, T1[] array1) |
| | 0 | 1420 | | { |
| | 0 | 1421 | | int indexToClaim = FreeIndex; |
| | 0 | 1422 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1423 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1424 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1425 | | |
| | 0 | 1426 | | array0[Count] = obj0; |
| | 0 | 1427 | | array1[Count] = obj1; |
| | | 1428 | | |
| | 0 | 1429 | | ++Count; |
| | 0 | 1430 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1431 | | |
| | 0 | 1432 | | return indexToClaim; |
| | 0 | 1433 | | } |
| | | 1434 | | |
| | | 1435 | | /// <summary> |
| | | 1436 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1437 | | /// </summary> |
| | | 1438 | | /// <returns>The sparse index allocated</returns> |
| | | 1439 | | public int AddUnchecked<T0, T1, T2>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, T2[] array2) |
| | 0 | 1440 | | { |
| | 0 | 1441 | | int indexToClaim = FreeIndex; |
| | 0 | 1442 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1443 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1444 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1445 | | |
| | 0 | 1446 | | array0[Count] = obj0; |
| | 0 | 1447 | | array1[Count] = obj1; |
| | 0 | 1448 | | array2[Count] = obj2; |
| | | 1449 | | |
| | 0 | 1450 | | ++Count; |
| | 0 | 1451 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1452 | | |
| | 0 | 1453 | | return indexToClaim; |
| | 0 | 1454 | | } |
| | | 1455 | | |
| | | 1456 | | /// <summary> |
| | | 1457 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1458 | | /// </summary> |
| | | 1459 | | /// <returns>The sparse index allocated</returns> |
| | | 1460 | | public int AddUnchecked<T0, T1, T2, T3>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, T2[] array2, |
| | | 1461 | | T3 obj3, |
| | | 1462 | | T3[] array3) |
| | 0 | 1463 | | { |
| | 0 | 1464 | | int indexToClaim = FreeIndex; |
| | 0 | 1465 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1466 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1467 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1468 | | |
| | 0 | 1469 | | array0[Count] = obj0; |
| | 0 | 1470 | | array1[Count] = obj1; |
| | 0 | 1471 | | array2[Count] = obj2; |
| | 0 | 1472 | | array3[Count] = obj3; |
| | | 1473 | | |
| | 0 | 1474 | | ++Count; |
| | 0 | 1475 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1476 | | |
| | 0 | 1477 | | return indexToClaim; |
| | 0 | 1478 | | } |
| | | 1479 | | |
| | | 1480 | | /// <summary> |
| | | 1481 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1482 | | /// </summary> |
| | | 1483 | | /// <returns>The sparse index allocated</returns> |
| | | 1484 | | public int AddUnchecked<T0, T1, T2, T3, T4>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, T2[] array2, |
| | | 1485 | | T3 obj3, T3[] array3, T4 obj4, T4[] array4) |
| | 0 | 1486 | | { |
| | 0 | 1487 | | int indexToClaim = FreeIndex; |
| | 0 | 1488 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1489 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1490 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1491 | | |
| | 0 | 1492 | | array0[Count] = obj0; |
| | 0 | 1493 | | array1[Count] = obj1; |
| | 0 | 1494 | | array2[Count] = obj2; |
| | 0 | 1495 | | array3[Count] = obj3; |
| | 0 | 1496 | | array4[Count] = obj4; |
| | | 1497 | | |
| | 0 | 1498 | | ++Count; |
| | 0 | 1499 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1500 | | |
| | 0 | 1501 | | return indexToClaim; |
| | 0 | 1502 | | } |
| | | 1503 | | |
| | | 1504 | | /// <summary> |
| | | 1505 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1506 | | /// </summary> |
| | | 1507 | | /// <returns>The sparse index allocated</returns> |
| | | 1508 | | public int AddUnchecked<T0, T1, T2, T3, T4, T5>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, |
| | | 1509 | | T2[] array2, |
| | | 1510 | | T3 obj3, T3[] array3, T4 obj4, T4[] array4, T5 obj5, T5[] array5) |
| | 0 | 1511 | | { |
| | 0 | 1512 | | int indexToClaim = FreeIndex; |
| | 0 | 1513 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1514 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1515 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1516 | | |
| | 0 | 1517 | | array0[Count] = obj0; |
| | 0 | 1518 | | array1[Count] = obj1; |
| | 0 | 1519 | | array2[Count] = obj2; |
| | 0 | 1520 | | array3[Count] = obj3; |
| | 0 | 1521 | | array4[Count] = obj4; |
| | 0 | 1522 | | array5[Count] = obj5; |
| | | 1523 | | |
| | 0 | 1524 | | ++Count; |
| | 0 | 1525 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1526 | | |
| | 0 | 1527 | | return indexToClaim; |
| | 0 | 1528 | | } |
| | | 1529 | | |
| | | 1530 | | /// <summary> |
| | | 1531 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1532 | | /// </summary> |
| | | 1533 | | /// <returns>The sparse index allocated</returns> |
| | | 1534 | | public int AddUnchecked<T0, T1, T2, T3, T4, T5, T6>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, |
| | | 1535 | | T2[] array2, |
| | | 1536 | | T3 obj3, T3[] array3, T4 obj4, T4[] array4, T5 obj5, T5[] array5, T6 obj6, T6[] array6) |
| | 0 | 1537 | | { |
| | 0 | 1538 | | int indexToClaim = FreeIndex; |
| | 0 | 1539 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1540 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1541 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1542 | | |
| | 0 | 1543 | | array0[Count] = obj0; |
| | 0 | 1544 | | array1[Count] = obj1; |
| | 0 | 1545 | | array2[Count] = obj2; |
| | 0 | 1546 | | array3[Count] = obj3; |
| | 0 | 1547 | | array4[Count] = obj4; |
| | 0 | 1548 | | array5[Count] = obj5; |
| | 0 | 1549 | | array6[Count] = obj6; |
| | | 1550 | | |
| | 0 | 1551 | | ++Count; |
| | 0 | 1552 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1553 | | |
| | 0 | 1554 | | return indexToClaim; |
| | 0 | 1555 | | } |
| | | 1556 | | |
| | | 1557 | | /// <summary> |
| | | 1558 | | /// Adds to the set without checking if the set needs to expand. |
| | | 1559 | | /// </summary> |
| | | 1560 | | /// <returns>The sparse index allocated</returns> |
| | | 1561 | | public int AddUnchecked<T0, T1, T2, T3, T4, T5, T6, T7>(T0 obj0, T0[] array0, T1 obj1, T1[] array1, T2 obj2, |
| | | 1562 | | T2[] array2, T3 obj3, T3[] array3, T4 obj4, T4[] array4, T5 obj5, T5[] array5, T6 obj6, T6[] array6, |
| | | 1563 | | T7 obj7, T7[] array7) |
| | 0 | 1564 | | { |
| | 0 | 1565 | | int indexToClaim = FreeIndex; |
| | 0 | 1566 | | int nextFreeIndex = SparseArray[indexToClaim]; |
| | 0 | 1567 | | DenseArray[Count] = indexToClaim; // Point the next dense id at our newly claimed sparse index. |
| | 0 | 1568 | | SparseArray[indexToClaim] = Count; // Point our newly claimed sparse index at the dense index. |
| | | 1569 | | |
| | 0 | 1570 | | array0[Count] = obj0; |
| | 0 | 1571 | | array1[Count] = obj1; |
| | 0 | 1572 | | array2[Count] = obj2; |
| | 0 | 1573 | | array3[Count] = obj3; |
| | 0 | 1574 | | array4[Count] = obj4; |
| | 0 | 1575 | | array5[Count] = obj5; |
| | 0 | 1576 | | array6[Count] = obj6; |
| | 0 | 1577 | | array7[Count] = obj7; |
| | | 1578 | | |
| | 0 | 1579 | | ++Count; |
| | 0 | 1580 | | FreeIndex = nextFreeIndex; // Set the free list head for next time. |
| | | 1581 | | |
| | 0 | 1582 | | return indexToClaim; |
| | 0 | 1583 | | } |
| | | 1584 | | |
| | | 1585 | | public void RemoveUnchecked<T0>(int sparseIndexToRemove, T0[] array0) |
| | 0 | 1586 | | { |
| | 0 | 1587 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1588 | | int newLength = Count - 1; |
| | 0 | 1589 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1590 | | |
| | | 1591 | | // Swap the entry being removed with the last entry. |
| | 0 | 1592 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1593 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1594 | | array0[denseIndexToRemove] = array0[newLength]; |
| | | 1595 | | |
| | | 1596 | | // Clear the dense index, for debugging purposes |
| | 0 | 1597 | | DenseArray[newLength] = -1; |
| | 0 | 1598 | | array0[newLength] = default; |
| | | 1599 | | |
| | | 1600 | | // Add the sparse index to the free list. |
| | 0 | 1601 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1602 | | FreeIndex = sparseIndexToRemove; |
| | | 1603 | | |
| | 0 | 1604 | | Count = newLength; |
| | 0 | 1605 | | } |
| | | 1606 | | |
| | | 1607 | | public void RemoveUnchecked<T0, T1>(int sparseIndexToRemove, T0[] array0, T1[] array1) |
| | 0 | 1608 | | { |
| | 0 | 1609 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1610 | | int newLength = Count - 1; |
| | 0 | 1611 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1612 | | |
| | | 1613 | | // Swap the entry being removed with the last entry. |
| | 0 | 1614 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1615 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1616 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1617 | | array1[denseIndexToRemove] = array1[newLength]; |
| | | 1618 | | |
| | | 1619 | | // Clear the dense index, for debugging purposes |
| | 0 | 1620 | | DenseArray[newLength] = -1; |
| | 0 | 1621 | | array0[newLength] = default; |
| | 0 | 1622 | | array1[newLength] = default; |
| | | 1623 | | |
| | | 1624 | | // Add the sparse index to the free list. |
| | 0 | 1625 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1626 | | FreeIndex = sparseIndexToRemove; |
| | | 1627 | | |
| | 0 | 1628 | | Count = newLength; |
| | 0 | 1629 | | } |
| | | 1630 | | |
| | | 1631 | | public void RemoveUnchecked<T0, T1, T2>(int sparseIndexToRemove, T0[] array0, T1[] array1, T2[] array2) |
| | 0 | 1632 | | { |
| | 0 | 1633 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1634 | | int newLength = Count - 1; |
| | 0 | 1635 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1636 | | |
| | | 1637 | | // Swap the entry being removed with the last entry. |
| | 0 | 1638 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1639 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1640 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1641 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 0 | 1642 | | array2[denseIndexToRemove] = array2[newLength]; |
| | | 1643 | | |
| | | 1644 | | // Clear the dense index, for debugging purposes |
| | 0 | 1645 | | DenseArray[newLength] = -1; |
| | 0 | 1646 | | array0[newLength] = default; |
| | 0 | 1647 | | array1[newLength] = default; |
| | 0 | 1648 | | array2[newLength] = default; |
| | | 1649 | | |
| | | 1650 | | // Add the sparse index to the free list. |
| | 0 | 1651 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1652 | | FreeIndex = sparseIndexToRemove; |
| | | 1653 | | |
| | 0 | 1654 | | Count = newLength; |
| | 0 | 1655 | | } |
| | | 1656 | | |
| | | 1657 | | public void RemoveUnchecked<T0, T1, T2, T3>(int sparseIndexToRemove, T0[] array0, T1[] array1, T2[] array2, |
| | | 1658 | | T3[] array3) |
| | 0 | 1659 | | { |
| | 0 | 1660 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1661 | | int newLength = Count - 1; |
| | 0 | 1662 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1663 | | |
| | | 1664 | | // Swap the entry being removed with the last entry. |
| | 0 | 1665 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1666 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1667 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1668 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 0 | 1669 | | array2[denseIndexToRemove] = array2[newLength]; |
| | 0 | 1670 | | array3[denseIndexToRemove] = array3[newLength]; |
| | | 1671 | | |
| | | 1672 | | // Clear the dense index, for debugging purposes |
| | 0 | 1673 | | DenseArray[newLength] = -1; |
| | 0 | 1674 | | array0[newLength] = default; |
| | 0 | 1675 | | array1[newLength] = default; |
| | 0 | 1676 | | array2[newLength] = default; |
| | 0 | 1677 | | array3[newLength] = default; |
| | | 1678 | | |
| | | 1679 | | // Add the sparse index to the free list. |
| | 0 | 1680 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1681 | | FreeIndex = sparseIndexToRemove; |
| | | 1682 | | |
| | 0 | 1683 | | Count = newLength; |
| | 0 | 1684 | | } |
| | | 1685 | | |
| | | 1686 | | public void RemoveUnchecked<T0, T1, T2, T3, T4>(int sparseIndexToRemove, T0[] array0, T1[] array1, T2[] array2, |
| | | 1687 | | T3[] array3, T4[] array4) |
| | 0 | 1688 | | { |
| | 0 | 1689 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1690 | | int newLength = Count - 1; |
| | 0 | 1691 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1692 | | |
| | | 1693 | | // Swap the entry being removed with the last entry. |
| | 0 | 1694 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1695 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1696 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1697 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 0 | 1698 | | array2[denseIndexToRemove] = array2[newLength]; |
| | 0 | 1699 | | array3[denseIndexToRemove] = array3[newLength]; |
| | 0 | 1700 | | array4[denseIndexToRemove] = array4[newLength]; |
| | | 1701 | | |
| | | 1702 | | // Clear the dense index, for debugging purposes |
| | 0 | 1703 | | DenseArray[newLength] = -1; |
| | 0 | 1704 | | array0[newLength] = default; |
| | 0 | 1705 | | array1[newLength] = default; |
| | 0 | 1706 | | array2[newLength] = default; |
| | 0 | 1707 | | array3[newLength] = default; |
| | 0 | 1708 | | array4[newLength] = default; |
| | | 1709 | | |
| | | 1710 | | // Add the sparse index to the free list. |
| | 0 | 1711 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1712 | | FreeIndex = sparseIndexToRemove; |
| | | 1713 | | |
| | 0 | 1714 | | Count = newLength; |
| | 0 | 1715 | | } |
| | | 1716 | | |
| | | 1717 | | public void RemoveUnchecked<T0, T1, T2, T3, T4, T5>(int sparseIndexToRemove, T0[] array0, T1[] array1, |
| | | 1718 | | T2[] array2, |
| | | 1719 | | T3[] array3, T4[] array4, T5[] array5) |
| | 0 | 1720 | | { |
| | 0 | 1721 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1722 | | int newLength = Count - 1; |
| | 0 | 1723 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1724 | | |
| | | 1725 | | // Swap the entry being removed with the last entry. |
| | 0 | 1726 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1727 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1728 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1729 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 0 | 1730 | | array2[denseIndexToRemove] = array2[newLength]; |
| | 0 | 1731 | | array3[denseIndexToRemove] = array3[newLength]; |
| | 0 | 1732 | | array4[denseIndexToRemove] = array4[newLength]; |
| | 0 | 1733 | | array5[denseIndexToRemove] = array5[newLength]; |
| | | 1734 | | |
| | | 1735 | | // Clear the dense index, for debugging purposes |
| | 0 | 1736 | | DenseArray[newLength] = -1; |
| | 0 | 1737 | | array0[newLength] = default; |
| | 0 | 1738 | | array1[newLength] = default; |
| | 0 | 1739 | | array2[newLength] = default; |
| | 0 | 1740 | | array3[newLength] = default; |
| | 0 | 1741 | | array4[newLength] = default; |
| | 0 | 1742 | | array5[newLength] = default; |
| | | 1743 | | |
| | | 1744 | | // Add the sparse index to the free list. |
| | 0 | 1745 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1746 | | FreeIndex = sparseIndexToRemove; |
| | | 1747 | | |
| | 0 | 1748 | | Count = newLength; |
| | 0 | 1749 | | } |
| | | 1750 | | |
| | | 1751 | | public void RemoveUnchecked<T0, T1, T2, T3, T4, T5, T6>(int sparseIndexToRemove, T0[] array0, T1[] array1, |
| | | 1752 | | T2[] array2, T3[] array3, T4[] array4, T5[] array5, T6[] array6) |
| | 0 | 1753 | | { |
| | 0 | 1754 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1755 | | int newLength = Count - 1; |
| | 0 | 1756 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1757 | | |
| | | 1758 | | // Swap the entry being removed with the last entry. |
| | 0 | 1759 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1760 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1761 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1762 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 0 | 1763 | | array2[denseIndexToRemove] = array2[newLength]; |
| | 0 | 1764 | | array3[denseIndexToRemove] = array3[newLength]; |
| | 0 | 1765 | | array4[denseIndexToRemove] = array4[newLength]; |
| | 0 | 1766 | | array5[denseIndexToRemove] = array5[newLength]; |
| | 0 | 1767 | | array6[denseIndexToRemove] = array6[newLength]; |
| | | 1768 | | |
| | | 1769 | | // Clear the dense index, for debugging purposes |
| | 0 | 1770 | | DenseArray[newLength] = -1; |
| | 0 | 1771 | | array0[newLength] = default; |
| | 0 | 1772 | | array1[newLength] = default; |
| | 0 | 1773 | | array2[newLength] = default; |
| | 0 | 1774 | | array3[newLength] = default; |
| | 0 | 1775 | | array4[newLength] = default; |
| | 0 | 1776 | | array5[newLength] = default; |
| | 0 | 1777 | | array6[newLength] = default; |
| | | 1778 | | |
| | | 1779 | | // Add the sparse index to the free list. |
| | 0 | 1780 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1781 | | FreeIndex = sparseIndexToRemove; |
| | | 1782 | | |
| | 0 | 1783 | | Count = newLength; |
| | 0 | 1784 | | } |
| | | 1785 | | |
| | | 1786 | | public void RemoveUnchecked<T0, T1, T2, T3, T4, T5, T6, T7>(int sparseIndexToRemove, T0[] array0, T1[] array1, |
| | | 1787 | | T2[] array2, T3[] array3, T4[] array4, T5[] array5, T6[] array6, T7[] array7) |
| | 0 | 1788 | | { |
| | 0 | 1789 | | int denseIndexToRemove = SparseArray[sparseIndexToRemove]; |
| | 0 | 1790 | | int newLength = Count - 1; |
| | 0 | 1791 | | int sparseIndexBeingSwapped = DenseArray[newLength]; |
| | | 1792 | | |
| | | 1793 | | // Swap the entry being removed with the last entry. |
| | 0 | 1794 | | SparseArray[sparseIndexBeingSwapped] = denseIndexToRemove; |
| | 0 | 1795 | | DenseArray[denseIndexToRemove] = sparseIndexBeingSwapped; |
| | 0 | 1796 | | array0[denseIndexToRemove] = array0[newLength]; |
| | 0 | 1797 | | array1[denseIndexToRemove] = array1[newLength]; |
| | 0 | 1798 | | array2[denseIndexToRemove] = array2[newLength]; |
| | 0 | 1799 | | array3[denseIndexToRemove] = array3[newLength]; |
| | 0 | 1800 | | array4[denseIndexToRemove] = array4[newLength]; |
| | 0 | 1801 | | array5[denseIndexToRemove] = array5[newLength]; |
| | 0 | 1802 | | array6[denseIndexToRemove] = array6[newLength]; |
| | 0 | 1803 | | array7[denseIndexToRemove] = array7[newLength]; |
| | | 1804 | | |
| | | 1805 | | // Clear the dense index, for debugging purposes |
| | 0 | 1806 | | DenseArray[newLength] = -1; |
| | 0 | 1807 | | array0[newLength] = default; |
| | 0 | 1808 | | array1[newLength] = default; |
| | 0 | 1809 | | array2[newLength] = default; |
| | 0 | 1810 | | array3[newLength] = default; |
| | 0 | 1811 | | array4[newLength] = default; |
| | 0 | 1812 | | array5[newLength] = default; |
| | 0 | 1813 | | array6[newLength] = default; |
| | 0 | 1814 | | array7[newLength] = default; |
| | | 1815 | | |
| | | 1816 | | // Add the sparse index to the free list. |
| | 0 | 1817 | | SparseArray[sparseIndexToRemove] = FreeIndex; |
| | 0 | 1818 | | FreeIndex = sparseIndexToRemove; |
| | | 1819 | | |
| | 0 | 1820 | | Count = newLength; |
| | 0 | 1821 | | } |
| | | 1822 | | |
| | | 1823 | | #endregion |
| | | 1824 | | } |
| | | 1825 | | } |